* Set up data


** Uncomment all to run. NOTE: very big data

/* ******************************************************************************************
* Grab and process the medpar data 

* cd /disk/aging/medicare/data/100pct/med

* foreach YEAR of num 1999/2016{
*  	disp `YEAR'

*  	cd /disk/aging/medicare/data/100pct/med
*  	use `YEAR'/1/med`YEAR', clear

* 	** Keep only necessary columns
* 	keep bene_id orgnpinm prvdrnum admsndt dschrgdt drg_cd pmt_amt

* 	* Save in my folder
* 	save /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/medpar/medpar`YEAR'.dta 
* }
* clear

* ** Put into one file

* cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/medpar
* use medpar1999.dta
* foreach YEAR of num 2000/2016{
* 	disp `YEAR'
* 	append using medpar`YEAR'.dta
* }
* save medpar9916.dta

* ************************************************
* **** Panelize by provider

* cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/medpar
* use prvdrnum dschrgdt pmt_amt using medpar9916.dta, clear

* gen dt = mofd(dschrgdt)
* encode prvdrnum, gen(group)

* collapse (sum) pmt_amt, by(prvdrnum dt)

* save /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/medpar/providerpanel.dta, replace

* ************************************************
* **** Tag defendant providers
* cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/medpar
* use providerpanel.dta, clear

* replace prvdrnum = "StJo" if prvdrnum == "110082"
* replace prvdrnum = "Wheaton" if prvdrnum =="241304"
* replace prvdrnum = "ElCentro" if prvdrnum == "050045"
* replace prvdrnum = "Overlook" if prvdrnum == "310051"
* replace prvdrnum = "MortonPlant" if inlist(prvdrnum, "100127", "100075", "100063", "100067", "100265", "100043")
* replace prvdrnum = "Shands" if inlist(prvdrnum, "100001", "100113", "100082", "100102", "101310", "101301")

* ** Tag CHS hospitals by a list of CCNs stored in a file
* merge m:1 prvdrnum using chsccn.dta
* replace prvdrnum = "chs" if chs == 1
* drop _merge chs


* *** Recollapse so 1 series per provider
* collapse (sum) pmt_amt, by(prvdrnum dt)
* save providerpanel_dft.dta, replace 

************************************************
**** Make plots for defendant providers
** Sued 11/16/2004
* cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop
* use medpar/providerpanel_dft.dta, clear 
* drop if dt > mofd(mdy(12,31,2016))

* local StJoTreat = mofd(mdy(11,16,2004))
* local StJoSettle = mofd(mdy(12,21,2007))
* twoway scatter pmt_amt dt if prvdrnum == "StJo", ylab(#3) xline(`StJoTreat') xline(`StJoSettle') saving("graphs/StJo")

* local WheatonTreat = mofd(mdy(10,19,2004))
* local WheatonSettle = mofd(mdy(1,7,2010))
* twoway scatter pmt_amt dt if prvdrnum == "Wheaton", ylab(#3) xline(`WheatonTreat') xline(`WheatonSettle') saving("graphs/Wheaton")

* local ElCentroTreat = mofd(mdy(3,20,2006))
* local ElCentroSettle = mofd(mdy(9,20,2010))
* twoway scatter pmt_amt dt if prvdrnum == "ElCentro", ylab(#3) xline(`ElCentroTreat') xline(`ElCentroSettle') saving("graphs/ElCentro")

* local OverlookTreat = mofd(mdy(4,25,2008))
* local OverlookSettle = mofd(mdy(6,21,2012))
* twoway scatter pmt_amt dt if prvdrnum == "Overlook", ylab(#3) xline(`OverlookTreat') xline(`OverlookSettle') saving("graphs/Overlook")

* local MortonPlantTreat = mofd(mdy(8,27,2008)) 
* local MortonPlantSettle = mofd(mdy(11,20,2012))
* twoway scatter pmt_amt dt if prvdrnum == "MortonPlant", ylab(#3) xline(`MortonPlantTreat') xline(`MortonPlantSettle') saving("graphs/MortonPlant")

* local ShandsTreat = mofd(mdy(8,30,2008)) 
* local ShandsSettle = mofd(mdy(8,8,2013))
* twoway scatter pmt_amt dt if prvdrnum == "Shands", ylab(#3) xline(`ShandsTreat') xline(`ShandsSettle') saving("graphs/Shands")

* local chsTreat = mofd(mdy(1,7,2009))
* local chsSettle= mofd(mdy(8,4,2014))
* twoway scatter pmt_amt dt if prvdrnum == "chs", ylab(#3) xline(`chsTreat') xline(`chsSettle') saving("graphs/chs")

* graph combine "graphs/StJo" "graphs/Wheaton" "graphs/ElCentro" "graphs/Overlook" "graphs/MortonPlant" "graphs/Shands" "graphs/chs"
* graph export "graphs/dftpanelgraph.pdf"
************************************************ */
************************************************
** Controls: all hospitals in non-treated states, without missing data
cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/medpar
use providerpanel_dft.dta, clear
drop if dt > mofd(mdy(12,31,2016))

** Filter by state 
gen provider_state = substr(prvdrnum, 1, 2)
gen control_state = 1 if  provider_state == "06" | provider_state == "07"|provider_state == "09" | provider_state == "12" | provider_state == "13"|provider_state == "16" | provider_state == "17" | provider_state == "20"|provider_state == "21"| provider_state == "22" | provider_state == "23"|provider_state == "27"
replace control_state = 1 if provider_state == "28" | provider_state == "29"|provider_state == "30"| provider_state == "33" | provider_state == "35"|provider_state == "36" | provider_state == "38" | provider_state == "41"|provider_state == "43" | provider_state == "47" | provider_state == "52"
replace control_state = 0 if mi(control_state)
keep if control_state == 1| inlist(prvdrnum, "StJo", "Wheaton", "ElCentro", "Overlook", "MortonPlant", "Shands", "chs")
drop provider_state

* Encode into temp groups to fill in panel 
encode prvdrnum, gen(g)
label var pmt_amt "Total Inpatient Payments"
label var dt "Month"
format dt %tm

tsset g dt 
tsfill, full
replace pmt_amt = 0 if mi(pmt_amt)
tsset g dt

** Tag controls by prvdrnum
** First need to fill in prvdrnum for empty panel

** Sort groups by time, carryforward provider number
sort g dt 
by g: carryforward prvdrnum, replace

** Sort groups by reverse time, carryforward provider number
count if mi(prvdrnum)
gen temp = 1000 - dt 
sort g temp 
by g: carryforward prvdrnum, replace 
count if mi(prvdrnum)
assert (r(N) == 0)
drop temp 

*** Tag controls by prvdrnum
gen control = 0 if inlist(prvdrnum, "StJo", "Wheaton", "ElCentro", "Overlook", "MortonPlant", "Shands", "chs")
replace control = 1 if!inlist(prvdrnum, "StJo", "Wheaton", "ElCentro", "Overlook", "MortonPlant", "Shands", "chs") 
count if mi(control)
assert(r(N) == 0)


*** Drop controls with missing months
bysort g: egen minmonth = min(pmt_amt)
drop if control == 1 & minmonth <= 0 
drop g 

** Wheaton data start later, 2000 month 7 
** Drop their pre-period 0s 
replace pmt_amt = . if prvdrnum == "Wheaton" & dt <=  mofd(mdy(6,1,2000))



save /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/ipop_controls_panel.dta, replace



*********************************************************
*** Need graph of full inpatient series to show no global effects
** 

cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/medpar
use medpar9916.dta
gen dt = mofd(dschrgdt)
collapse (sum) pmt_amt, by(dt)

local WheatonTreat = mofd(mdy(10,19,2004))
drop if mi(dt)
drop if dt > mofd(mdy(12,31,2016))

format dt %tm_Mon_YY
twoway scatter pmt_amt dt, xline(`WheatonTreat') yscale(range(0 16500000000)) ylab(0 5000000000 "$5 Bil" 10000000000 "$10 Bil" 15000000000 "$15 Bil") xlab(468 492 516 `WheatonTreat' 564 588 612 636 660) xtitle("Month") ytitle("$/Month") graphregion(color(white)) 
graph export /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/graphs/AllIP.pdf

